/**
 * 
 */
package com.paypal.melearning.ui.component;

import com.paypal.melearning.domain.Comment;
import com.paypal.melearning.domain.ContentType;
import com.paypal.melearning.domain.Pin;
import com.vaadin.event.MouseEvents.ClickEvent;
import com.vaadin.event.MouseEvents.ClickListener;
import com.vaadin.terminal.ClassResource;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;

/**
 * @author prakrishnan
 * 
 */
public class PinUI extends VerticalLayout {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6793886921265332731L;

	private Pin curPin = new Pin();

	private final String url = "http://www.google.com";

	public PinUI(Pin pin) {
		super();
		this.curPin = pin;
		setMargin(true);
		setSpacing(true);
		setSizeFull();
		// setWidth("30%");
		// setHeight("100%");
	}

	public void render(Pin pin) {
		if (ContentType.IMAGE.name().equals(pin.getType())) {
			renderImagePin(pin);
		} else if (ContentType.VIDEO.name().equals(pin.getType())) {
			renderVideoPin(pin);
		} else if (ContentType.WEB.name().equals(pin.getType())) {
			renderWebPin(pin);
		} else {
			renderPin(pin, pin.getType());
		}
	}

	private void renderWebPin(final Pin pin) {

		final Embedded web = new Embedded(pin.getPinName(),
				new ExternalResource(pin.getContentURL()));
		web.setType(Embedded.TYPE_BROWSER);
		web.setStandby("Loading Pls Wait");
		web.setSizeFull();
		web.setWidth("200px");
		web.setHeight("265px");

		web.addListener(new ClickListener() {

			/**
			 * 
			 */
			private static final long serialVersionUID = -7541319743055222613L;

			public void click(ClickEvent event) {
				web.getWindow().open(
						new ExternalResource(pin.getContentURL()), "_blank");

			}
		});

		addComponent(web);

		// VerticalLayout commentsPannel = new VerticalLayout();
		Label comment = new Label();
		comment.setCaption(pin.getDescription());
		addComponent(comment);

		addComments(pin);
		System.out.println("Finished Render Pin");

	}

	@Override
	public void attach() {
		super.attach();
		render(curPin);
	}

	private void renderPin(final Pin pin, String type) {
		System.out.println("Within Render Pin");
		Label name = new Label(pin.getPinName());
		name.setSizeFull();

		ClassResource pdfImg = new ClassResource("/pdf-icon.jpg",
				getApplication());

		ClassResource pptImg = new ClassResource("/ppt-icon.jpg",
				getApplication());

		ClassResource docImg = new ClassResource("/doc-icon.jpg",
				getApplication());

		final Embedded img = new Embedded(pin.getPinName());
		img.setWidth("200px");
		// ClassResource r = new ClassResource("/sample.jpg", getApplication());
		if (ContentType.PDF.name().equals(type)) {
			img.setSource(pdfImg);
		} else if (ContentType.PPT.name().equals(type)) {
			img.setSource(pptImg);
		} else {
			img.setSource(docImg);
		}

		img.addListener(new ClickListener() {

			/**
			 * 
			 */
			private static final long serialVersionUID = -7541319743055222613L;

			public void click(ClickEvent event) {
				img.getWindow().open(new ExternalResource(pin.getContentURL()),
						"_blank");

			}
		});

		addComponent(img);

		// VerticalLayout commentsPannel = new VerticalLayout();
		Label comment = new Label();
		comment.setCaption(pin.getDescription());
		addComponent(comment);

		addComments(pin);
		System.out.println("Finished Render Pin");
	}

	private void addComments(Pin pin) {
		for (Comment comments : pin.getComment()) {
			Label comment = new Label(comments.getComment());
			addComponent(comment);
		}
	}

	private void renderVideoPin(final Pin pin) {

		System.out.println("Within Render Pin");
		Label name = new Label(pin.getPinName());
		name.setSizeFull();

		final Embedded video = new Embedded(pin.getPinName(),
				new ExternalResource(pin.getContentURL()));

		video.setMimeType("application/x-shockwave-flash");
		video.setParameter("allowFullScreen", "true");

		video.setSizeFull();
		video.setWidth("200px");
		video.setHeight("265px");

		video.addListener(new ClickListener() {

			/**
			 * 
			 */
			private static final long serialVersionUID = -7541319743055222613L;

			public void click(ClickEvent event) {
				video.getWindow().open(new ExternalResource(pin.getContentURL()), "_blank");

			}
		});

		addComponent(video);

		// VerticalLayout commentsPannel = new VerticalLayout();
		Label comment = new Label();
		comment.setCaption(pin.getDescription());
		addComponent(comment);

		addComments(pin);
		System.out.println("Finished Render Pin");

	}

	private void renderImagePin(final Pin pin) {

		Label name = new Label(pin.getPinName());
		name.setSizeFull();

		final Embedded image = new Embedded(pin.getPinName(),
				new ExternalResource(pin.getContentURL()));
		image.setWidth("200px");

		image.addListener(new ClickListener() {
			/**
			 * 
			 */

			private static final long serialVersionUID = -7541319743055222613L;

			public void click(ClickEvent event) {
				image.getWindow().open(
						new ExternalResource(pin.getContentURL()), "_blank");

			}
		});

		addComponent(image);

		// VerticalLayout commentsPannel = new VerticalLayout();
		Label comment = new Label();
		comment.setCaption(pin.getDescription());
		addComponent(comment);

		addComments(pin);
		System.out.println("Finished Render Pin");

	}
}
